<template>
  <div id='areaChart' class='chartPart'></div>
</template>

<script>
import { areaData } from '@/api/index'
export default {
  data () {
    return {
      areaData: [],
      alarmData: [
        {
          name: '低',
          data: []
        },
        {
          name: '中',
          data: []
        },
        {
          name: '高',
          data: []
        },
        {
          name: '紧急',
          data: []
        }
      ],
      alarmLevel: ['低', '中', '高', '紧急']
    }
  },
  methods: {
    init () {
      this.areaData = []
      // 情况数据
      for (let i = 0; i < this.alarmData.length; i++) {
        this.alarmData[i].data = []
      }
      // 请求告警数据
      areaData().then((res) => {
        if (res.code === 200) {
          this.alarmLevel = this.alarmLevel.reverse()
          res.data.map((item) => {
            // 区域数据
            this.areaData.push(item.area)
            // 告警
            for (let k in item) {
              if (k === 'emergency') {
                this.alarmData[3].data.push(item[k])
              } else if (k === 'high') {
                this.alarmData[2].data.push(item[k])
              } else if (k === 'intermediate') {
                this.alarmData[1].data.push(item[k])
              } else if (k === 'low') {
                this.alarmData[0].data.push(item[k])
              }
            }
          })
        }
        if (this.areaData) this.areaChart()
      })
    },
    areaChart () {
      let that = this
      let color = ['#7cabe9', '#71b5cd', '#e5ad88', '#c16354']
      // 柱状图数据处理
      const alarmSeries = function () {
        let data = []
        that.alarmData.map((item, index) => {
          let str = {
            type: 'bar',
            barWidth: '16%',
            itemStyle: {
              normal: {
                color: color[index],
                barBorderRadius: [5, 5, 0, 0]
              }
            }
            // label: {
            //   normal: {
            //     show: true,
            //     position: 'top',
            //     fontSize: 14,
            //     fontWeight: 600
            //   }
            // }
          }
          item = {...item, ...str}
          data.push(item)
        })
        return data
      }
      var option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          left: '2%',
          right: '4%',
          bottom: '5%',
          top: '16%',
          containLabel: true
        },
        legend: {
          data: ['低', '中', '高', '紧急'],
          right: 10,
          top: 20,
          textStyle: {
            color: '#666'
          },
          itemWidth: 15,
          itemHeight: 10
        },
        xAxis: {
          type: 'category',
          data: this.areaData,
          axisLine: {
            lineStyle: {
              color: '#666'
            }
          },
          axisTick: {show: false},
          axisLabel: {
            textStyle: {
              fontFamily: 'Microsoft YaHei'
            }
          }
        },
        yAxis: {
          type: 'value',
          axisLine: {
            show: false,
            lineStyle: {
              color: '#666'
            }
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: 'rgba(0, 0, 0, 0.3)'
            }
          },
          axisTick: {show: false},
          axisLabel: {
            textStyle: {
              color: '#333'
            }
          }
        },
        series: alarmSeries()
      }
      var myChart = this.$echarts.init(document.getElementById('areaChart'))
      myChart.setOption(option)
      // 触发一次图表加载避免显示隐藏后图形宽高变小
      myChart.resize()
      window.addEventListener('resize', function () {
        myChart.resize()
      })
    }
  }
}
</script>

<style lang='scss' scoped>
#mapChart {
  width: 100%;
  height: 100%;
}
</style>
